package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanhang.common.util.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.entity.Departments;
import com.nanhang.entity.Employees;
import com.nanhang.service.DepartmentsService;
import com.nanhang.service.EmployeesService;
import com.nanhang.mapper.EmployeesMapper;
import com.nanhang.vo.EmployeesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 虾大大
* @description 针对表【employees】的数据库操作Service实现
* @createDate 2025-01-10 21:10:16
*/
@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements EmployeesService{
    @Autowired
    private DepartmentsService departmentsService;
    @Override
    public PageResult<Employees> findByPage(Integer page,Integer pageSize){
        // 1. 开始分页
        Page<Employees> page1 = this.page(new Page<>(page,pageSize));
// 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 3. 条件带分页查询
    @Override
    public PageResult<Employees> search(Integer page, Integer pageSize, EmployeesVo vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Employees> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getName()), "Name",  vo.getName() )
                .like(StrUtil.isNotBlank(vo.getSex()), "Sex", vo.getSex())
                .eq(vo.getDeptid() != null && vo.getDeptid() != 0, "Deptid", vo.getDeptid());
        // 1.2 开始分页查询
        Page<Employees> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 1.3 处理所在部门
        page1.getRecords().forEach(st -> {
            Departments departments = departmentsService.getById(st.getDeptid());
            st.setDeptname(departments.getDeptname());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    // 添加
    @Override
    public boolean add(Employees employees) {
        return this.save(employees);
    }
}




